package com.example.work8_1.Controller;


import com.example.work8_1.Exception.ProviderException;
import com.example.work8_1.Service.ProviderService;
import com.example.work8_1.entity.Provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("provider")
public class ProviderController {
    @Autowired
    ProviderService ps;


    @GetMapping("/{id}")
    public ResponseEntity<Provider> getSupplier(@PathVariable Integer id){
        Provider result = ps.findProvider(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @PostMapping("")
    @PreAuthorize("hasAnyAuthority('Administration')")
    public Provider addProduct(@RequestBody Provider p)throws ProviderException
    {
        return ps.addProvider(p);
    }


    @PutMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('Administration')")
    public void updateProduct(@PathVariable Integer id,@RequestBody Provider p)throws ProviderException
    {
         ps.updateProvider(id,p);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('Administration')")
    public void deleteProduct(@PathVariable Integer id)throws ProviderException{
        ps.deleteProvider(id);
    }
}
